#!/usr/bin/env python
# -*- coding: utf8 -*-
# title       :
# description :
# author      :'ShenMeng'

from PySide import QtGui
import uuid
from .pyuic.list_group_widget import Ui_list_group_widget
from .list_item_widget import ListItemWidget

class ListGroupWidget(QtGui.QWidget):
    def __init__(self, parent=None, group_name='', data=None):
        super(ListGroupWidget, self).__init__(parent)
        self.current_item = None
        self._data = data
        self._group_name = group_name
        self._ui = Ui_list_group_widget()
        self._ui.setupUi(self)
        frame_id= uuid.uuid4().hex
        self._ui.frame.setObjectName(frame_id)
        self.setStyleSheet('''
            *{border:None;background-color:rgb(0,0,0,0);}
            QFrame::hover#%s{background-color:rgb(0,0,0,15);}
            ''' % frame_id)
        self.current_icon = QtGui.QIcon(":/icons/expand_down.png")
        self.item_list = []
        self._ui.engine_list_frame.setVisible(0)
        self._ui.frame.mousePressEvent = self.expend_item
        self._ui.expend_icon.clicked.connect(self.expend_item)
        self.add_item()
        self._set_group_name(self._group_name.capitalize())
        self._ui.group_count.setVisible(1)

    def _set_group_name(self, name):
        self._ui.group_name.setText("""
        <html><head/><body><p><span style=" font-weight:600;">%s</span></p></body></html>
        """ % name)

    # def add_item(self):
    #     if not self._data:
    #         return
    #     engine_num = len(self._data)
    #     self._set_group_count(engine_num)
    #     for engine in self._data:
    #         engine_item = ListItemWidget(self)
    #         engine_item.set_config(self._group_name, self._data.get(engine), engine)
    #         self._ui.engine_layout.addWidget(engine_item)
    def add_item(self):
        if not self._data:
            return
        engine_num = len(self._data)
        self._set_group_count(engine_num)
        for engine_data in self._data:
            engine_item = ListItemWidget(self)
            engine_item.set_config(self._group_name, engine_data)
            self._ui.engine_layout.addWidget(engine_item)

    def _set_group_count(self, num):
        self._ui.group_count.setText("""
        <html><head/><body><p><span style=" font-weight:600;">%s</span></p></body></html>
        """ % str(num))

    def clear_item(self):
        for item in self.item_list:
            item.close()

    def expend_item(self, *arg):
        if self._ui.engine_list_frame.isVisible():
            self._ui.engine_list_frame.setVisible(0)
        else:
            self._ui.engine_list_frame.setVisible(1)
        icon = self._ui.expend_icon.icon()
        self._ui.expend_icon.setIcon(self.current_icon)
        self.current_icon = icon

    # def mouseReleaseEvent(self, event):
    #     self.expend_item()
